package com.realbrand.net.realbrandpay.adapter;

import android.content.Context;
import android.graphics.Typeface;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.realbrand.net.realbrandpay.R;
import com.realbrand.net.realbrandpay.api.HTTPAPI;
import com.realbrand.net.realbrandpay.bean.AllMallCarBean;
import com.realbrand.net.realbrandpay.bean.CommodityBean;
import com.realbrand.net.realbrandpay.inter.IUpdateDealSum;
import com.realbrand.net.realbrandpay.untils.HttpRequest;
import com.realbrand.net.realbrandpay.untils.Static;
import com.realbrand.net.realbrandpay.weight.SmoothCheckBox;
import com.realbrand.net.realbrandpay.ui.activity.mall.IViewUpdata;
import com.realbrand.net.realbrandpay.zxing.util.L;
import com.squareup.picasso.Picasso;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Administrator on 2018/5/14.
 */

public class MallCarAdapter extends BaseExpandableListAdapter {
    private LayoutInflater inflater;
    private Context mContext;
    private IViewUpdata updateViewListener;
    private AllMallCarBean allMallCarBean;
    protected static final int KEY_DATA = 0xFFF11133;
    private boolean addCommodity;
    private View.OnClickListener onAddNum;//加商品数量的接口
    private View.OnClickListener onRemove;//删除整个item的接口

    public void setOnAddNum(View.OnClickListener onAddNum) {
        this.onAddNum = onAddNum;
    }

    public void setOnRemove(View.OnClickListener onRemove) {
        this.onRemove = onRemove;
    }

    public MallCarAdapter(Context mContext, AllMallCarBean allMallCarBean) {
        this.mContext = mContext;
        this.allMallCarBean = allMallCarBean;
        inflater = LayoutInflater.from(mContext);
    }

    @Override
    public int getGroupCount() {
        return allMallCarBean.getContent().size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return allMallCarBean.getContent().get(groupPosition).getList().size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return allMallCarBean.getContent().get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return allMallCarBean.getContent().get(groupPosition).getList().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GroupViewHolder groupViewHolder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.mallcarlitsview_itemone, parent, false);
            groupViewHolder = new GroupViewHolder(convertView);
            convertView.setTag(groupViewHolder);
        } else {
            groupViewHolder = (GroupViewHolder) convertView.getTag();
        }
        groupViewHolder.mallCar_elv_item_one_cb_brand.setTag(groupPosition);
        groupViewHolder.mallCar_elv_item_one_cb_brand.setOnClickListener(listener);
        groupViewHolder.mallCar_elv_item_one_tv_brand.setText(allMallCarBean.getContent().get(groupPosition).getBrandName());
        Picasso.with(mContext).load(allMallCarBean.getContent().get(groupPosition).getBrandLog()).into(groupViewHolder.mallCar_elv_item_one_iv_brand);
        //根据获取的状态设置是否被选中
        if (allMallCarBean.getContent().get(groupPosition).isSelect()) {
            if (!groupViewHolder.mallCar_elv_item_one_cb_brand.isChecked()) {
                groupViewHolder.mallCar_elv_item_one_cb_brand.setChecked(true);
            }
        } else {
            groupViewHolder.mallCar_elv_item_one_cb_brand.setChecked(false);
        }
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ChildViewHolder childViewHolder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.mallcarlistview_itemtow, parent, false);
            childViewHolder = new ChildViewHolder(convertView);
            childViewHolder.mallCar_elv_item_two_ib_commodityAdd.setOnClickListener(onAddNum);
            childViewHolder.mallCar_elv_item_two_ib_commodityDelete.setOnClickListener(onRemove);
            convertView.setTag(childViewHolder);
        } else {
            childViewHolder = (ChildViewHolder) convertView.getTag();
        }
        String tag = groupPosition + "," + childPosition;
        childViewHolder.mallCar_elv_item_two_cb_commodity.setTag(tag);
        childViewHolder.mallCar_elv_item_two_ib_commodityDelete.setTag(tag);
        childViewHolder.mallCar_elv_item_two_ib_commodityAdd.setTag(tag);
        childViewHolder.mallCar_elv_item_two_cb_commodity.setOnClickListener(listener);
//        childViewHolder.mallCar_elv_item_two_ib_commodityDelete.setOnClickListener(listener);
        //添加商品数量
//        childViewHolder.mallCar_elv_item_two_ib_commodityAdd.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//
//
//            }
//        });
        //根据获取的状态设置是否被选中
        if (allMallCarBean.getContent().get(groupPosition).getList().get(childPosition).isCommoditySelect()) {
            childViewHolder.mallCar_elv_item_two_cb_commodity.setChecked(true);
        } else {
            childViewHolder.mallCar_elv_item_two_cb_commodity.setChecked(false);
        }
        //设置数据
        childViewHolder.mallCar_elv_item_two_tv_commodityPrice.setText(allMallCarBean.getContent().get(groupPosition).getList().get(childPosition).getCommodityPrice());
        childViewHolder.mallCar_elv_item_two_tv_commodity.setText(allMallCarBean.getContent().get(groupPosition).getList().get(childPosition).getCommodityName());
        String[] split = allMallCarBean.getContent().get(groupPosition).getList().get(childPosition).getCommodityLog().split(",");
        Picasso.with(mContext).load(split[0]+split[1]).into(childViewHolder.mallCar_elv_item_two_iv_commodity);
        //对商品数量的监听
        EditTextWatcher textWatcher = (EditTextWatcher) childViewHolder.mallCar_elv_item_two_et_commoditySum.getTag(KEY_DATA);
        if (textWatcher != null) {
            childViewHolder.mallCar_elv_item_two_et_commoditySum.removeTextChangedListener(textWatcher);
        }
        childViewHolder.mallCar_elv_item_two_et_commoditySum.setText(allMallCarBean.getContent().get(groupPosition).getList().get(childPosition).getCommoditySum());
        EditTextWatcher watcher = new EditTextWatcher(allMallCarBean.getContent().get(groupPosition).getList().get(childPosition));
        childViewHolder.mallCar_elv_item_two_et_commoditySum.setTag(KEY_DATA, watcher);
        childViewHolder.mallCar_elv_item_two_et_commoditySum.addTextChangedListener(watcher);
        childViewHolder.mallCar_elv_item_two_et_commoditySum.setText(allMallCarBean.getContent().get(groupPosition).getList().get(childPosition).getCommoditySum());
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }

    public void setChangedListener(IViewUpdata listener) {
        if (updateViewListener == null) {
            this.updateViewListener = listener;
        }
    }


    /**
     * 商品数量EditText内容改变的监听
     */
    class EditTextWatcher implements TextWatcher {

        private CommodityBean GoodDetail;

        public EditTextWatcher(CommodityBean item) {
            this.GoodDetail = item;
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            if (!TextUtils.isEmpty(s.toString().trim())) {
                String textNum = s.toString().trim();
                GoodDetail.setCommoditySum(textNum);
            }
        }
    }

    View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            SmoothCheckBox checkBox;
            String tag = v.getTag().toString();
            String[] split;
            int groupId = 0;
            int childId = 0;
            int childSize = 0;
            int groupPosition = 0;
            int allCount = allMallCarBean.getAllCount();//被选中的Item数量
            double allMoney = allMallCarBean.getAllMoney();
            if (tag.contains(",")) {
                split = tag.split(",");
                groupId = Integer.parseInt(split[0]);
                childId = Integer.parseInt(split[1]);
            } else {
                groupPosition = Integer.parseInt(tag);
                childSize = allMallCarBean.getContent().get(groupPosition).getList().size();
            }
            switch (v.getId()) {
                case R.id.mallCar_lv_itemOne_cb_brand:
                    checkBox = (SmoothCheckBox) v;
                    //根据父CheckBox的选中状态设置存储数据里面商品是否被选中
                    allMallCarBean.getContent().get(groupPosition).setSelect(!checkBox.isChecked());
                    if (!checkBox.isChecked()) {
                        for (int i = 0; i < childSize; i++) {
                            if (!allMallCarBean.getContent().get(groupPosition).getList().get(i).isCommoditySelect()) {
                                allCount++;
                                allMallCarBean.getContent().get(groupPosition).getList().get(i).setCommoditySelect(!checkBox.isChecked());
                                allMoney += Double.parseDouble(allMallCarBean.getContent().get(groupPosition).getList().get(i).getCommoditySum())
                                        * Double.parseDouble(allMallCarBean.getContent().get(groupPosition).getList().get(i).getCommodityPrice());
                            }
                        }
                    } else {
                        allCount -= childSize;
                        for (int i = 0; i < childSize; i++) {
                            allMallCarBean.getContent().get(groupPosition).getList().get(i).setCommoditySelect(!checkBox.isChecked());
                            allMoney -= Double.parseDouble(allMallCarBean.getContent().get(groupPosition).getList().get(i).getCommoditySum())
                                    * Double.parseDouble(allMallCarBean.getContent().get(groupPosition).getList().get(i).getCommodityPrice());
                        }
                    }
                    //父Item选中的数量
                    int fCount = 0;
                    //判断是否所以的父item都被选中，决定全选按钮状态
                    for (int i = 0; i < allMallCarBean.getContent().size(); i++) {
                        if (allMallCarBean.getContent().get(i).isSelect()) {
                            fCount++;
                        }
                    }
                    if (fCount == allMallCarBean.getContent().size()) {
                        allMallCarBean.setAllSelect(true);
                    } else {
                        allMallCarBean.setAllSelect(false);
                    }
                    allMallCarBean.setAllCount(allCount);
                    allMallCarBean.setAllMoney(allMoney);
                    notifyDataSetChanged();
                    updateViewListener.update(allMallCarBean.isAllSelect(), allCount, allMoney);
                    break;
                //单个子项Item被点击
                case R.id.mallCar_lv_itemTwo_cb_commodity:
                    checkBox = (SmoothCheckBox) v;
                    int cCount = 0;//子Item被选中的数量
                    int fcCount = 0;//父Item被选中的数量
                    allMallCarBean.getContent().get(groupId).getList().get(childId).setCommoditySelect(!checkBox.isChecked());
                    //遍历父item所有数据，统计被选中的item数量
                    for (int i = 0; i < allMallCarBean.getContent().get(groupId).getList().size(); i++) {
                        if (allMallCarBean.getContent().get(groupId).getList().get(i).isCommoditySelect()) {
                            cCount++;
                        }
                    }
                    //判断是否所以的字Item都被选中，决定父Item状态
                    if (cCount == allMallCarBean.getContent().get(groupId).getList().size()) {
                        allMallCarBean.getContent().get(groupId).setSelect(true);
                    } else {
                        allMallCarBean.getContent().get(groupId).setSelect(false);
                    }
                    //判断是否所以的父item都被选中，决定全选按钮状态
                    for (int i = 0; i < allMallCarBean.getContent().size(); i++) {
                        if (allMallCarBean.getContent().get(i).isSelect()) {
                            fcCount++;
                        }
                    }
                    if (fcCount == allMallCarBean.getContent().size()) {
                        allMallCarBean.setAllSelect(true);
                    } else {
                        allMallCarBean.setAllSelect(false);
                    }
                    //判断子Item状态，更新结算总商品数和合计money
                    if (!checkBox.isChecked()) {
                        allCount++;
                        allMoney += Double.parseDouble(allMallCarBean.getContent().get(groupId).getList().get(childId).getCommoditySum())
                                * Double.parseDouble(allMallCarBean.getContent().get(groupId).getList().get(childId).getCommodityPrice());
                    } else {
                        allCount--;
                        allMoney -= Double.parseDouble(allMallCarBean.getContent().get(groupId).getList().get(childId).getCommoditySum())
                                * Double.parseDouble(allMallCarBean.getContent().get(groupId).getList().get(childId).getCommodityPrice());
                    }
                    allMallCarBean.setAllCount(allCount);
                    allMallCarBean.setAllMoney(allMoney);
                    notifyDataSetChanged();
                    updateViewListener.update(allMallCarBean.isAllSelect(), allCount, allMoney);
                    break;
//                case R.id.mallCar_lv_itemTwo_ib_delete:
//                    //减少商品数量
//                    String goodCount = allMallCarBean.getContent().get(groupId).getList().get(childId).getCommoditySum();
//                    if (Integer.valueOf(goodCount) > 1) {
//                        allMallCarBean.getContent().get(groupId).getList().get(childId).setCommoditySum(reduceCount(goodCount));
//                        if (allMallCarBean.getContent().get(groupId).getList().get(childId).isCommoditySelect()) {
//                            allMoney -= Integer.valueOf(allMallCarBean.getContent().get(childId).getList().get(childId).getCommodityPrice());
//                            updateViewListener.update(allMallCarBean.isAllSelect(), allCount, allMoney);
//                        }
//                        allMallCarBean.setAllMoney(allMoney);
//                        notifyDataSetChanged();
//                    }
//                    break;

            }
        }
    };

    private String reduceCount(String var) {
        Integer integer = Integer.valueOf(var);
        if (integer > 1) {
            integer--;
        }
        return integer + "";
    }

    private String addCount(String var) {
        Integer integer = Integer.valueOf(var);
        integer++;
        return integer + "";
    }

    class GroupViewHolder {
        SmoothCheckBox mallCar_elv_item_one_cb_brand;
        ImageView mallCar_elv_item_one_iv_brand;
        TextView mallCar_elv_item_one_tv_brand;

        GroupViewHolder(View view) {
            mallCar_elv_item_one_cb_brand = (SmoothCheckBox) view.findViewById(R.id.mallCar_lv_itemOne_cb_brand);
            mallCar_elv_item_one_iv_brand = (ImageView) view.findViewById(R.id.mallCar_lv_itemOne_iv_brand);
            mallCar_elv_item_one_tv_brand = (TextView) view.findViewById(R.id.mallCar_lv_itemOne_tv_brand);
            mallCar_elv_item_one_tv_brand.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
        }
    }

    class ChildViewHolder {
        SmoothCheckBox mallCar_elv_item_two_cb_commodity;
        ImageView mallCar_elv_item_two_iv_commodity;
        TextView mallCar_elv_item_two_tv_commodity;
        TextView mallCar_elv_item_two_tv_commodityPrice;
        EditText mallCar_elv_item_two_et_commoditySum;
        ImageButton mallCar_elv_item_two_ib_commodityAdd;
        ImageButton mallCar_elv_item_two_ib_commodityDelete;

        ChildViewHolder(View view) {
            mallCar_elv_item_two_cb_commodity = (SmoothCheckBox) view.findViewById(R.id.mallCar_lv_itemTwo_cb_commodity);
            mallCar_elv_item_two_iv_commodity = (ImageView) view.findViewById(R.id.mallCar_lv_itemTwo_iv_commodity);
            mallCar_elv_item_two_tv_commodity = (TextView) view.findViewById(R.id.mallCar_listView_itemTwo_product);
            mallCar_elv_item_two_tv_commodityPrice = (TextView) view.findViewById(R.id.mallCar_lv_itemTwo_tv_money);
            mallCar_elv_item_two_et_commoditySum = (EditText) view.findViewById(R.id.mallCar_lv_itemTwo_et_sum);
            mallCar_elv_item_two_ib_commodityAdd = (ImageButton) view.findViewById(R.id.mallCar_lv_itemTwo_ib_add);
            mallCar_elv_item_two_ib_commodityDelete = (ImageButton) view.findViewById(R.id.mallCar_lv_itemTwo_ib_delete);
        }
    }


}
